# ggplot2 - boxplot
library(ggplot2)
library(ggthemes)
library(ggpubr)

# Boxplot
ggplot(mpg, aes(class, displ)) + geom_boxplot()
ggboxplot(mpg, x = "class", y = "displ")

# Boxplot with notch
ggplot(mpg, aes(class, displ)) + 
  geom_boxplot(aes(fill = class), notch = TRUE) + # Add notch info and points
  geom_jitter(binaxis = "y", position = position_jitter(0.3), stackdir = "center", dotsize = 0.4) +
  theme_light()

ggboxplot(mpg, x = "class", y = "displ", fill = "class",
          notch = TRUE, add = "dotplot")

ggboxplot(mpg, x = "class", y = "displ",
          add = "jitter", shape = "class", fill = "class")

# Violin and boxplot
ggplot(mpg, aes(class, displ)) +
  geom_violin(aes(fill = class), trim = FALSE) +
  geom_jitter(aes(color = class), width = 0.1) +
  geom_boxplot(width = 0.2) +
  coord_flip() +
  theme_light()

# Facet
ggplot(diamonds, aes(cut, price, fill = color)) + geom_boxplot()

ggplot(diamonds, aes(cut, price, fill = color)) + 
  geom_boxplot() +
  facet_grid(. ~ color)

# Same as above (ggpubr)
ggboxplot(diamonds, x = "cut", y = "price", fill = "color",
          facet.by = "color")

ggplot(diamonds, aes(cut, price, fill = color)) + 
  geom_boxplot() +
  facet_wrap(~ color)

ggplot(diamonds, aes(cut, price, fill = color)) + 
  geom_boxplot() +
  ggtitle("Boxplot") + 
  theme_wsj() + scale_fill_wsj() + 
  guides(fill = guide_legend(title = NULL))
